package com.yzt.zhuju;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.yzt.loginmodule.login.OneKeyLoginActivity;
import com.yzt.zhuju.arouter.RouterAccount;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.p_base.utils.SharedPreferenceUtil;
import com.yzt.zhuju.rx.AsyncCall;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * author : bob
 * date   : 2022/11/28
 * desc   : 启动页
 */
public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        start();
    }

    private void start() {
        Observable.intervalRange(0, 4, 0, 1_000, TimeUnit.MILLISECONDS)
                .compose(new AsyncCall<>())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(@NonNull Long value) {
                        if (value == 2) {
                            goToNext();
                        }
                    }
                });
    }

    private void goToNext() {
        if (RXingUtils.isLogin()) {
            boolean isRegister = SharedPreferenceUtil.getInstance().getValue(RXingUtils.LOGIN_REGISTER, false);
//            if (isRegister) {
                RouterMain.MainActivity();
//            } else {
//                SharedPreferenceUtil.getInstance().putValue(RXingUtils.LOGIN_REGISTER, true);
//                RouterAccount.RegisterChooseGenderActivity();
//            }
        } else {
            OneKeyLoginActivity.startSelf(this);
        }

        finish();
    }
}
